import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
	login,
	logout,
	getInfo
} from '@/api/login'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'
import {
	auth
} from '@/api/auth.js'
import {
	createWebSocket,
	closeWebSocket
} from '@/utils/socket.js'

const user = {
	state: {
		token: getToken(),
		name: storage.get(constant.name),
		avatar: storage.get(constant.avatar),
		userId: storage.get(constant.userId),
		teacher: storage.get(constant.teacher), // 是否是老师端登录，true为老师端，false为学生端，默认false
		userInfo: storage.get(constant.userInfo),
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_NAME: (state, name) => {
			state.name = name
			storage.set(constant.name, name)
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
			storage.set(constant.avatar, avatar)
		},
		SET_USERID: (state, userId) => {
			state.userId = userId
			storage.set(constant.userId, userId)
		},
		SET_TEACHER: (state, webTeacher) => {
			state.teacher = webTeacher
			storage.set(constant.teacher, webTeacher)
		},
		SET_USERINFO: (state, userInfo) => {
			state.userInfo = userInfo
			storage.set(constant.userInfo, userInfo)
		}
	},

	actions: {
		// 登录
		Login({
			commit
		}, userInfo) {
			const account = userInfo.account
			const password = userInfo.pwd
			return new Promise((resolve, reject) => {
				login(account, password).then(resp => {
					setToken(resp.token)
					// createWebSocket(account)
					commit('SET_TOKEN', resp.token)
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},
		/**
		 * 更新邀请人
		 */
		UpdateInvite({
			commit
		}, code) {
			return new Promise((resolve, reject) => {
				if (!code) {
					reject('邀请码不存在')
					return
				}
				// updateInvite(code).then(resp => {
				// 	resolve(resp.data.inviteCode)
				// })
			})
		},

		// 授权登录
		AuthLogin({
			commit
		}, code) {
			return new Promise((resolve, reject) => {
				auth(code).then(respones => {
					if (respones.token) {
						setToken(respones.token)
						// createWebSocket()
						commit('SET_TOKEN', respones.token)
					}
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getInfo().then(res => {
					let user = res.data.userInfo
					//讲师角色设置
					user.role = "User"
					const teacher = res.data.teacher
					const fundAccount = res.data.fundAccount
					const webTeacher = res.data.webTeacher || {}
					const webEnt = res.data.webEnt
					if (Object.keys(webTeacher).length > 0) {
						user.role = "Teacher"
						commit('SET_TEACHER', webTeacher)
					}
					commit('SET_USERINFO', {
						userId: user.userId,
						userName: user.userName,
						nickName: user.nickName,
						openId: user.openId,
						phonenumber: user.phonenumber,
						avatar: user.avatar,
						company: user.company,
						job: user.job
					})
					commit('SET_NAME', user.userName)
					commit('SET_AVATAR', user.avatar)
					commit('SET_USERID', user.userId)
					resolve({
						user,
						webTeacher,
						webEnt,
					})
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				logout(state.token).then(() => {
					commit('SET_TOKEN', '')
					commit('SET_TEACHER', {})
					removeToken()
					closeWebSocket()
					storage.clean()
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}
	}
}

export default user